﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class city_s_show_picture : System.Web.UI.Page
{
    int no;


    protected void Page_Load(object sender, EventArgs e)
    {

        no = int.Parse(Request["no"]);




        if (!this.IsPostBack)
        {
            ctpDataContext db = new ctpDataContext();

            ct_s_pic pic = db.ct_s_pics.Single(p => p.no == no);

            ct_s_profile cts = db.ct_s_profiles.Single(p => p.valid == true && p.no == pic.ct_s_no && p.language_no == (int)Session["now_user_lang_no"]);

            this.HyperLink3.Text = cts.title;
            this.HyperLink3.NavigateUrl = "city_s.aspx?no=" + cts.no.ToString();
            this.HyperLink2.Text = cts.ct_profile.title.Trim();
            this.HyperLink2.NavigateUrl = "city.aspx?no=" + cts.ct_no;

            this.Image1.ImageUrl = "city_imgs/2/"+pic.filename.Trim();

            if (pic.user.profile_picture != null)
            {
                this.Image2.ImageUrl = "user_imgs/1/" + pic.user.profile_picture.Trim();
            }
            this.HyperLink9.Text = pic.user.nickname.Trim();
            this.HyperLink9.NavigateUrl = "user_profile.aspx?no=" + pic.create_user_no.ToString();


            var piccomment = from p in db.ct_s_picture_comments
                             where p.ct_s_no == no
                             select p;

            foreach (ct_s_picture_comment c in piccomment)
            {
                if (c.user.profile_picture == null)
                    c.user.profile_picture = "";
                else
                    c.user.profile_picture = c.user.profile_picture.Trim();

            }

            this.DataList1.DataSource = piccomment;
            this.DataBind();

            this.Label2.Text= piccomment.Count().ToString();

            this.ViewState["like_amount"] = piccomment.Count();
            this.ViewState["cts_no"] = cts.no;
            this.ViewState["filename"] = pic.filename;
            this.ViewState["ct_no"] = cts.ct_no;

        }

        if (Request["like"] != null)
        {
            like();
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ctp_lib.check_login(this.Page, "city_s_show_picture.aspx?like=1&no=" + no.ToString());

        like();


    }

    void like()
    {
        ctpDataContext db = new ctpDataContext();

        int alreadylike = db.ct_s_picture_comments.Count(p => p.ct_s_no == no && p.create_user_no == (int)Session["userno"]);
        if (alreadylike != 0) return;


        ct_s_picture_comment comm = new ct_s_picture_comment();
        comm.create_user_no = (int)Session["userno"];
        comm.ct_s_no = no;
        comm.addtime = DateTime.Now;

        db.ct_s_picture_comments.InsertOnSubmit(comm);

        ct_s_pic pic = db.ct_s_pics.Single(p => p.no == no);
        pic.like_amount += 1;

        //chg cts_main_picture
        var maxdata = from p in db.ct_s_pics
                      where p.ct_s_no == (int)this.ViewState["cts_no"]
                      select p;

        int max=maxdata.Max(p => p.like_amount);

        if ((int)this.ViewState["like_amount"] >= max)
        {
            var cts = from p in db.ct_s_profiles
                      where p.valid == true && p.no == (int)this.ViewState["cts_no"]
                      select p;

            foreach (ct_s_profile p in cts)
            {
                p.main_pic_fn = (string)this.ViewState["filename"];

            }
        }

        //chg ct_main_picture
        ct_profile ct = db.ct_profiles.Single(p => p.no == (int)this.ViewState["ct_no"] && p.language_no == (int)Session["now_user_lang_no"]);
        if ((int)this.ViewState["like_amount"] >= ct.main_pic_fn_like_amount)
        {
            ct.main_pic_fn = (string)this.ViewState["filename"];
            ct.main_pic_fn_like_amount = (int)this.ViewState["like_amount"];
        }

        db.SubmitChanges();

        Response.Redirect("city_s_show_picture.aspx?no="+no.ToString());


    }
}
